<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Db;
/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
     
        $this->view->assign([
            'totaluser'        => $this->totaluser(),
            'totalp'       => $this->totalp(),
            'totale'       => $this->totale(),
            'totalorderamount' =>  $this->totalorderamount(),
            'todayuserlogin'   => $this->todayuserlogin(),
            'todayusersignup'  => $this->todayusersignup(),
            'todayorder'       =>  $this->todayorder(),
            'unsettleorder'    => $this->unsettleorder(),
            'sevendnu'         => $this->sevendnu(),
            'sevendau'         =>  $this->sevendau(),
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode,
            'fkyj'=>$this->fa_litestore_option(),
            'gyxxtj'=>$this->gyxxtj(),
            'qgxxtj'=>$this->qgxxtj(),
            'jswt'=>$this->jswt(),
            'jszl'=>$this->jszl()
        ]);

        return $this->view->fetch();
    }
    /**
     * 反馈意见记录总数
     */
    public function fa_litestore_option()
    {
       $fa_litestore_option = Db::query("select count(*) as fa_litestore_option from fa_litestore_option");
       return $fa_litestore_option[0]['fa_litestore_option'];
    }

    /**
     * 供应信息总记录数
     */
    public function gyxxtj()
    {
        $gyxxtj = Db::query("select count(*) as gyxxtj from fa_litestore_request where type='1'");
       return $gyxxtj[0]['gyxxtj'];
    }
    /**
     * 求购信息总记录数
     */
    public function qgxxtj()
    {
        $qgxxtj = Db::query("select count(*) as qgxxtj from fa_litestore_request where type='2'");
       return $qgxxtj[0]['qgxxtj'];
    }
    /**
     * 技术问题统计
     */
    public function jswt()
    {
        $jswt   = Db::query("select count(*) as jswt from fa_litestore_tech_question");
       return $jswt[0]['jswt'];
    }
    /**
     * 当前技术资料总记录数
     */
    public function jszl()
    {
        $jszl   = Db::query("select count(*) as jszl from fa_litestore_tech_data");
       return $jszl[0]['jszl'];
    }
    


    /**
     * 总会员数
     */
    public function totaluser(){
        $totaluser = Db::query("SELECT COUNT(*) AS totaluser FROM fa_user WHERE STATUS ='normal' AND group_id IN('1','2','3','4','5','6','7','8')");
        return $totaluser[0]['totaluser'];
    }
    /**
     * 个人会员数
     */
    public function totalp(){
        $totalp = Db::query("select count(*) as totaluser from  fa_user where STATUS ='normal' AND group_id in('7','1') ");
        return $totalp[0]['totaluser'];
    }

    /**
     * 企业会员数
     */
    public function totale(){
        $totale = Db::query("select count(*) as totaluser from fa_user where STATUS ='normal' AND group_id in('2','3','4','5','6','8') ");
        return $totale[0]['totaluser'];
    }
    /**
     * 订单总金额
     */
    public function totalorderamount(){
        $totalorderamount = Db::query("select sum(total_price) as totalorderamount from fa_litestore_service_order where order_status='30'");
        return empty($totalorderamount[0]['totalorderamount'])?'0.00':$totalorderamount[0]['totalorderamount'];
    }
    
    /**
     * 未处理订单数
     */
    public function unsettleorder()
    {
        $map =[];
        $map['order_status']  = '10';
        $unsettleorder = Db::name('litestore_service_order')->where($map)->count('id');
        return $unsettleorder;
    }
    /**
     * 今日订单数
     */
    public function todayorder(){
       
        $map =[];
        $startdt =mktime(0,0,0,date('m'),date('d'),date('Y'));
        $enddt =mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $map['createtime']  = array('BETWEEN',array($startdt,$enddt));
        $todayorder = Db::name('litestore_service_order')->where($map)->count('id');
        return $todayorder;
    }
    /**
     * 总订单数
     */
    public function sevendnu(){

        $sevendnu = Db::name('litestore_service_order')->count('id');
        return $sevendnu;
    }
    
    /**
     * 未交费会员数
     */
    public function sevendau()
    {
        $map =[];
        $map['level']  = '1';
        $map['status']  = 'normal';
        $unsettleorder = Db::name('user')->where($map)->count('id');
        return $unsettleorder;
    }
     /**
     * 今日登录
     */
    public function todayuserlogin(){
      $map =[];
      $startdt =mktime(0,0,0,date('m'),date('d'),date('Y'));
      $enddt =mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
      $map['logintime']  = array('BETWEEN',array($startdt,$enddt));
      $todayuserlogin = Db::name('third')->where($map)->count('id');
        return $todayuserlogin;
    }
    /**
     * 今日注册
     */
    public function todayusersignup(){
        $map =[];
        $map['createtime']  = array('BETWEEN',array(mktime(0,0,0,date('m'),date('d'),date('Y')),mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1));
        $todayusersignup = Db::name('user')->where($map)->count('id');
        return $todayusersignup;
    }
}
